<?php
/*
Primary Author: Derek Shue
Secondary: Alex Arey
Documentation: Cheng Yi
Updated: 12/10/2011
Module Description: The Users module adds/finds/updates/deletes Users from the database.
*/
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    protected $_name = 'USERS';

public function getUser($where)
    {

        $row = $this->fetchRow($where);
        if (!$row) {
            throw new Exception("Could not find row $where");
        }
		
	return $row->toArray();
    }

    public function addUser($loginname, $pw, $rank, $type)
    {
			
		$id = $this->getNextKey();
		
        $data = array(
            'ID' => $id,
            'LOGINNAME' => $loginname,
			'PW' => $pw,
			'RANK' => $rank,
			'TYPE' => $type,
			
        );
        $this->insert($data);
		return $id;
	}

    public function updateUser($id, $loginname, $pw, $rank, $type)
    {
        $data = array(
            'LOGINNAME' => $loginname,
			'PW' => $pw,
			'RANK' => $rank,
			'TYPE' => $type,
        );
		
		$data = array_filter($data);
        $this->update($data, 'ID = '. (int)$id);
    }

    public function deleteUser($id)
    {
        $this->delete('ID =' . (int)$id);
    }

	
	public function getNextKey(){
			$sqlquery = $this->select()->from($this->_name, array("MAX(ID)"));
			$insertrow = $this->fetchRow($sqlquery)->toArray(); 
			return $insertrow["MAX(ID)"] + 1;
	
	}


	
	}



?>
